package com.beagle.njddapp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.bean.CompanyBean;
import com.beagle.njddapp.bean.owner.NjBean;
import com.beagle.njddapp.bean.UploadBean;
import com.beagle.njddapp.bean.UserInfoBean;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.ui.presenter.UserInfoPresenter;
import com.beagle.njddapp.ui.view.UserInfoView;
import com.beagle.njddapp.utils.GlideEngine;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.beagle.njddapp.utils.Utils;
import com.beagle.njddapp.weight.dialog.CompanyDialog;
import com.beagle.njddapp.weight.dialog.GanderDIalog;
import com.beagle.njddapp.weight.dialog.NjListDIalog;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.beagle.njddapp.ui.activity.BasicUserInfoActivity.CHOOSE_IMG_BACK;
import static com.beagle.njddapp.ui.activity.BasicUserInfoActivity.CHOOSE_IMG_FRONT;
import static com.beagle.njddapp.ui.activity.InputActivity.NORMAL_ID;
import static com.beagle.njddapp.ui.activity.InputActivity.NORMAL_LONG;
import static com.beagle.njddapp.ui.activity.InputActivity.NORMAL_NUMBER;
import static com.beagle.njddapp.ui.activity.InputActivity.NORMAL_PHONE;
import static com.beagle.njddapp.ui.activity.InputActivity.NORMAL_SHORT;

/**
 * @author hxx
 * @date 2021-03-04 20:21
 * 用户信息修改
 */
public class UserInfoUpdateActivity extends BaseMvpActivity<UserInfoView, UserInfoPresenter> implements UserInfoView, CompanyDialog.CompanyDialogItemClickLisenter, NjListDIalog.onNjDialogItemClickLisenter {

    @BindView(R.id.commom_title_tv)
    TextView tvTitle;
    @BindView(R.id.common_container)
    RelativeLayout container;
    @BindView(R.id.common_tv_right)
    TextView tvRight;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.tv_identity)
    TextView tvCard;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_concat)
    TextView tvConcat;
    @BindView(R.id.tv_concat_phone)
    TextView tvConcatPhone;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_drive)
    TextView tvDirver;
    @BindView(R.id.iv1)
    ImageView iv1;
    @BindView(R.id.iv2)
    ImageView iv2;
    @BindView(R.id.tv_car)
    TextView tvCar;
    @BindView(R.id.rl_car)
    RelativeLayout rlCar;
    private CompanyDialog companyDialog;
    private UserInfoBean userInfoBean;
    private int type;
    private NjListDIalog njListDIalog;
    private List<NjBean> njBeanList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_userinfo_update;
    }

    @Override
    public void init() {
        StatusBarUtil.immersive(this, Color.WHITE);
        StatusBarUtil.darkMode(this);
        tvTitle.setText("个人信息");
        tvRight.setText("修改");
        userInfoBean = new UserInfoBean().coyp(UserService.getInstance().getUserInfoBean());
        tvName.setText(userInfoBean.getUserName());
        if (userInfoBean.getGender() == 1) {
            tvSex.setText("男");
        } else {
            tvSex.setText("女");
        }
        tvPhone.setText(userInfoBean.getPhone());
        tvCard.setText(userInfoBean.getIdcard());
        tvConcat.setText(userInfoBean.getEmergencyContact());
        tvConcatPhone.setText(userInfoBean.getEmergencyContactPhone());
        tvCompany.setText(userInfoBean.getCompany());
        tvAddress.setText(userInfoBean.getAddress());
        tvDirver.setText(userInfoBean.getDrivingAge() + "");
        tvCar.setText(userInfoBean.getOftenMechNum());
        Glide.with(this).load(userInfoBean.getLicenceUrlA()).apply(new RequestOptions().placeholder(R.mipmap.register_btn_drivea)).into(iv1);
        Glide.with(this).load(userInfoBean.getLicenceUrlB()).apply(new RequestOptions().placeholder(R.mipmap.register_btn_driveb)).into(iv2);
        njBeanList=new ArrayList<>();
        presenter.getDeviceListRun(1000,1);
        if (UserService.getInstance().getRole()==UserService.ROLE_OIL||UserService.getInstance().getRole()==UserService.ROLE_DIRVER){
            rlCar.setVisibility(View.GONE);
        }

    }

    @Override
    public UserInfoView getMvpView() {
        return this;
    }

    @Override
    public UserInfoPresenter createPresenter() {
        return new UserInfoPresenter();
    }

    @Override
    public void onError(String message) {
        ToastUtils.showShort(message);
    }

    @OnClick({R.id.common_back_iv, R.id.common_tv_right, R.id.rl_name, R.id.rl_sex, R.id.rl_identity, R.id.rl_adress, R.id.rl_concat
            , R.id.rl_concat_phone, R.id.rl_company, R.id.rl_drive, R.id.iv1, R.id.iv2, R.id.rl_car})
    public void onCLick(View view) {
        switch (view.getId()) {
            case R.id.common_back_iv:
                onBackPressed();
                break;
            case R.id.common_tv_right:
                presenter.Update(userInfoBean);
                break;
            case R.id.rl_name:
                startActivityForResult(new Intent(this, InputActivity.class).putExtra("data", tvName.getText().toString()).putExtra("type", NORMAL_SHORT), 101);
                break;
            case R.id.rl_sex:
                change_sex();
                break;
            case R.id.rl_identity:
                startActivityForResult(new Intent(this, InputActivity.class).putExtra("data", tvCard.getText().toString()).putExtra("type", NORMAL_ID), 102);
                break;
            case R.id.rl_adress:
                startActivityForResult(new Intent(this, InputActivity.class).putExtra("data", tvAddress.getText().toString()).putExtra("type", NORMAL_LONG), 103);
                break;
            case R.id.rl_concat:
                startActivityForResult(new Intent(this, InputActivity.class).putExtra("data", tvConcat.getText().toString()).putExtra("type", NORMAL_SHORT), 104);
                break;
            case R.id.rl_concat_phone:
                startActivityForResult(new Intent(this, InputActivity.class).putExtra("data", tvConcatPhone.getText().toString()).putExtra("type", NORMAL_PHONE), 105);
                break;
            case R.id.rl_company:
                presenter.getCompany();
                break;
            case R.id.rl_drive:
                startActivityForResult(new Intent(this, InputActivity.class).putExtra("data", tvDirver.getText().toString()).putExtra("type", NORMAL_NUMBER), 106);
                break;
            case R.id.iv1:
                Matisse.from(this)
                        .choose(MimeType.ofImage())
                        .capture(true)
                        .countable(true)
                        .maxSelectable(1)
                        .thumbnailScale(0.85f)
                        .captureStrategy(new CaptureStrategy(true, "com.beagle.njddapp"))
                        .imageEngine(GlideEngine.getInstance())
                        .showPreview(false) // Default is `true`
                        .forResult(CHOOSE_IMG_FRONT);

                break;
            case R.id.iv2:
                Matisse.from(this)
                        .choose(MimeType.ofImage())
                        .capture(true)
                        .countable(true)
                        .maxSelectable(1)
                        .thumbnailScale(0.85f)
                        .captureStrategy(new CaptureStrategy(true, "com.beagle.njddapp"))
                        .imageEngine(GlideEngine.getInstance())
                        .showPreview(false) // Default is `true`
                        .forResult(CHOOSE_IMG_BACK);
                break;
            case R.id.rl_car:
                if (njBeanList==null||njBeanList.size()==0){
                    ToastUtils.showShort("请先添加农机");
                    return;
                }
                if (njListDIalog==null){
                    njListDIalog=new NjListDIalog(this);
                    njListDIalog.setOnNjDialogItemClickLisenter(this);
                }
                njListDIalog.show();
                njListDIalog.setNjBeanList(njBeanList);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1006) {
            if (data == null) {
                return;
            }
            String str = data.getStringExtra("data");
            switch (requestCode) {
                case 101:
                    tvName.setText(str);
                    userInfoBean.setUserName(str);
                    break;
                case 102:
                    tvCard.setText(str);
                    userInfoBean.setIdcard(str);
                    break;
                case 103:
                    tvAddress.setText(str);
                    userInfoBean.setAddress(str);
                    break;
                case 104:
                    tvConcat.setText(str);
                    userInfoBean.setEmergencyContact(str);
                    break;
                case 105:
                    tvConcatPhone.setText(str);
                    userInfoBean.setEmergencyContactPhone(str);
                    break;
                case 106:
                    tvDirver.setText(str);
                    userInfoBean.setDrivingAge(Integer.parseInt(str));
                    break;
            }
        } else if (requestCode == CHOOSE_IMG_FRONT) {
            if (data==null){
                return;
            }
            List<Uri> mSelected = Matisse.obtainResult(data);
            if (mSelected != null && mSelected.size() > 0) {
                type=CHOOSE_IMG_FRONT;
                Glide.with(this).load(mSelected.get(0)).into(iv1);
                LogUtils.d(mSelected.get(0).getPath());
                presenter.upload(Utils.getRealFilePath(getApplicationContext(),mSelected.get(0)));
            }
        } else if (requestCode == CHOOSE_IMG_BACK) {
            if (data==null){
                return;
            }
            List<Uri> mSelected = Matisse.obtainResult(data);
            if (mSelected != null && mSelected.size() > 0) {
                type=CHOOSE_IMG_BACK;
                Glide.with(this).load(mSelected.get(0)).into(iv2);
                presenter.upload(Utils.getRealFilePath(getApplicationContext(),mSelected.get(0)));
            }
        }
    }

    public void change_sex() {
        GanderDIalog ganderDIalog = new GanderDIalog(this);
        ganderDIalog.setOnGanderChooseCallBack(new GanderDIalog.onGanderChooseCallBack() {
            @Override
            public void onGanderChoose(int gander_dia) {
                if (gander_dia == 1) {
                    tvSex.setText("男");
                } else {
                    tvSex.setText("女");
                }
                userInfoBean.setGender(gander_dia);

            }
        });
        ganderDIalog.show();
    }

    @Override
    public void success() {
        ToastUtils.showLong("修改成功");
        tvAddress.postDelayed(new Runnable() {
            @Override
            public void run() {
                setResult(505);
                finish();
            }
        },2000);

    }

    @Override
    public void uploadSuccess(UploadBean data) {
        if (type==CHOOSE_IMG_FRONT){
            userInfoBean.setLicenceUrlA(data.getVisitURL());
        }else {
            userInfoBean.setLicenceUrlB(data.getVisitURL());

        }

    }

    @Override
    public void showCampany(List<CompanyBean> data) {
        if (companyDialog==null){
            companyDialog=new CompanyDialog(this);
            companyDialog.setCompanyDialogItemClickLisenter(this);
        }
        companyDialog.show();
        companyDialog.setZdBeanList(data);
    }

    @Override
    public void showRunNj(List<NjBean> results) {
        njBeanList.addAll(results);

    }

    @Override
    public void onCpmpanyCLick(CompanyBean companyBean) {
        userInfoBean.setCompany(companyBean.getName());
        tvCompany.setText(companyBean.getName());
    }

    @Override
    public void onItemClick(NjBean njBean) {
        tvCar.setText(njBean.getMeshNum());
        userInfoBean.setOftenMechNum(njBean.getMeshNum());
    }
}
